using System.Drawing;
using System.Windows.Forms;
using Nuwanda.TfsSpotlight.Shell.Proxies.Interfaces;
using Nuwanda.TfsSpotlight.Shell.Services.Interfaces;
using StructureMap;

namespace Nuwanda.TfsSpotlight.Shell.Services
{
    [Pluggable("MonitService")]
    public class MonitService : IMonitService
    {
        private readonly IKryptonHeader _monitBar;
        private readonly IStatusBar _statusBar;

        public MonitService(IKryptonHeader monitBar, IStatusBar statusBar)
        {
            this._monitBar = monitBar;
            this._statusBar = statusBar;
        }

        /// <summary>Places given message on a status bar</summary>
        /// <param name="message">Masage to display</param>
        /// <param name="severity">Severity of the message</param>
        public void ShowStatusMessage(string message, MessageSeverity severity)
        {
            this._statusBar.SetMessage(message, GetIcon(severity));
        }

        /// <summary>Clears the status message</summary>
        public void ClearStatusMessage()
        {
            this._statusBar.ClearMessage();
        }

        private static Image GetIcon(MessageSeverity severity)
        {
            switch (severity)
            {
                case MessageSeverity.Information:
                    return Properties.Images.Information;
                case MessageSeverity.Warning:
                    return Properties.Images.Warning;
                case MessageSeverity.Error:
                case MessageSeverity.Exception:
                    return Properties.Images.Error;
                case MessageSeverity.Retrieving:
                    return Properties.Images.Retrieving;
                case MessageSeverity.Saving:
                    return Properties.Images.Saving;
                default: return Properties.Images.Information;
            }
        }

        /// <summary>Starts unknown progress. The duration is not defined and it looks as marque</summary>
        public void StartUnknownProgress()
        {
            this._statusBar.StartProgress(ProgressBarStyle.Marquee);
        }

        /// <summary>Stops progress</summary>
        public void StopProgress()
        {
            this._statusBar.StopProgress();
        }

        /// <summary>Shows message box with question</summary>
        /// <param name="question">Question</param>
        /// <param name="buttons">Buttons to show</param>
        /// <returns>User input</returns>
        public DialogResult ShowQuestion(string question, MessageBoxButtons buttons)
        {
            return this._statusBar.ShowQuestion(question, buttons);
        }

        public void ShowMonitMessage(string message, MessageSeverity severity)
        {
            this._monitBar.ShowMessage(message, GetIcon(severity));
        }

        /// <summary>Shows a message on a monit panel</summary>
        /// <param name="message">Message to show</param>
        /// <param name="proceedHandler">A handler to fire when user clicks OK</param>
        public void ShowMonitMessage(string message, MethodInvoker proceedHandler)
        {
            this._monitBar.ShowMessage(message);
        }

        /// <summary>Hides monit panel</summary>
        public void HideMonitMessage()
        {
            this._monitBar.Hide();
        }
    }
}